﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace BadHabit.Factory.Controls {

    /// <summary>
    /// Do vertical scroll without scroll bars.
    /// Make sure to set your panel ScrollViewer.VerticalScrollBarVisibility to Hidden in order to use this control.
    /// </summary>
    public partial class SmartScroll : UserControl {

        /// <summary>
        /// The button style.
        /// </summary>
        public Style RepeatButtonStyle { get; set; }

        /// <summary>
        /// Avaialable scroll directions.
        /// </summary>
        public enum ScrollDirections {
            Up,
            Down
        }

        /// <summary>
        /// Gets and sets the Scroll Direction.
        /// </summary>
        public ScrollDirections ScrollDirection {
            get {
                if (VerticalAlignment == VerticalAlignment.Top)
                    return ScrollDirections.Up;
                else
                    return ScrollDirections.Down;
            }
            set {
                if (ScrollDirections.Up == value) {
                    VerticalAlignment = VerticalAlignment.Top;                    
                }
                else
                    VerticalAlignment = VerticalAlignment.Bottom;
            }
        }

        /// <summary>
        /// Gets and sets the name of the panel to put the scroll.
        /// </summary>
        public string PanelName { get; set; }
       

        /// <summary>
        /// Gets the panel element.
        /// </summary>
        private FrameworkElement Panel {
            get {                
                FrameworkElement parent = this.Parent as FrameworkElement;                
                // due my ignoranse, or due a WPF BUG, FindName does not work here. Why?
                // return parent.FindName(PanelName) as FrameworkElement;
                // so i did the code bellow.
                int c = VisualTreeHelper.GetChildrenCount(parent);
                for (int i = 0; i <= c; i++) {
                    FrameworkElement e = VisualTreeHelper.GetChild(parent, i) as FrameworkElement;
                    if (e != null && PanelName.Equals(e.Name))
                        return e;
                }
                return null;
            }
        }

        /// <summary>
        /// Default Constructor.
        /// </summary>
        public SmartScroll() {            
            InitializeComponent();
            Loaded += new RoutedEventHandler(SmartScroll_Loaded);            
        }


        void SmartScroll_Loaded(object sender, RoutedEventArgs e) {
            scrollButton.Style = RepeatButtonStyle;

            if (ScrollViewer != null) {
                ScrollViewer.ScrollChanged -= new ScrollChangedEventHandler(ScrollViewer_ScrollChanged);
                ScrollViewer.ScrollChanged += new ScrollChangedEventHandler(ScrollViewer_ScrollChanged);
            } else {
                //scrollButton.Visibility = Visibility.Hidden;
            }
        }
        

        void ScrollViewer_ScrollChanged(object sender, ScrollChangedEventArgs e) {
            if (ScrollDirection == ScrollDirections.Up) {
                if (ScrollViewer.VerticalOffset == 0)
                    scrollButton.Visibility = Visibility.Hidden;
                else
                    scrollButton.Visibility = Visibility.Visible;

                
            }
            if (ScrollDirection == ScrollDirections.Down) {
                if (ScrollViewer.VerticalOffset == ScrollViewer.ScrollableHeight)
                    scrollButton.Visibility = Visibility.Hidden;
                else
                    scrollButton.Visibility = Visibility.Visible;

            }
        }

        private ScrollViewer scrollViewer = null;        

        /// <summary>
        /// Gets the ScrollViewer for the list box.
        /// </summary>
        private ScrollViewer ScrollViewer {
            get {
                if (scrollViewer != null)
                    return scrollViewer;

                if(Panel==null)
                    return null;
                Border scrollBorder = VisualTreeHelper.GetChild(Panel, 0) as Border;
                if (scrollBorder == null) return null;
                scrollViewer = scrollBorder.Child as ScrollViewer;

                return scrollViewer;
            }
        }

        /// <summary>
        /// Scrolls the panel.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void scrollButton_Click(object sender, RoutedEventArgs e) {

            
            if (ScrollViewer == null)
                return;
            if (ScrollDirections.Up == ScrollDirection)
                ScrollViewer.LineUp();
            else 
                ScrollViewer.LineDown();
            

           
        }        

    }
}
